// 商品详情信息

// 创建路由
const router = require('express').Router()
// 接口名称
const apiName = 'goods_details'
// 引入数据库
const db = require('../db.js')

// 商品信息
router.get(`/${apiName}/:id(\\d+)`, (req, res) => {
    let id = req.params.id;
    // console.log(id);

    let sql = `SELECT * FROM gm_goods WHERE id = ?`

    db.query(sql, id, (error, result) => {
        if (error) {
            res.json({
                "code": 400,
                "error": error
            })
        } else {
            res.json({
                "code": 200,
                "data": result
            })
        }
    })
})

// 商品加入购物车
router.post(`/${apiName}/join-cart`, (req, res) => {
    /*********** 接口数据 */
    let goods_id = req.body.goods_id || ''
    let num = req.body.num || ''
    let user_id = req.body.user_id || ''
    let price = req.body.price || ''
    let goods_name = req.body.goods_name || ''
    /* 插入到数据库中 */
    let sql = 'INSERT INTO gm_shopc SET ?'
    let data = {
        goods_id,
        num,
        user_id,
        price,
        goods_name
    }
    // 执行sql语句
    db.query(sql, data, (error, result) => {
        if (error) {
            res.json({
                "code": 400,
                "error": error
            })
        } else {
            res.json({
                "code": 200,
                "data": result
            })
        }
    })
})

// 根据id查询购物车里的商品
router.get(`/showCart/:id(\\d+)`, (req, res) => {
    let id = req.params.id;

    let sql = `SELECT * FROM gm_shopc WHERE goods_id = ?`

    db.query(sql, id, (error, result) => {
        if (error) {
            res.json({
                "code": 400,
                "error": error
            })
        } else {
            // 判断购物车是否有该商品
            if (result.length == 0) {
                res.json({
                    'code': 401
                })
            } else {
                let num = result[0].num + 1
                // console.log(result[0].num);
                let id = result[0].id
                // 修改将要购买的商品数量
                db.query('update gm_shopc set num = ? where id = ?', [num, id], (err, resu) => {
                    if (error) {
                        console.log(err);
                    } else {
                        res.json({
                            'code': 200
                        })
                    }
                })
            }
        }
    })
})

// 查询购物车
router.get(`/goods_cart`, (req, res) => {

    let sql = `SELECT * FROM gm_shopc`

    db.query(sql, (error, result) => {
        if (error) {
            res.json({
                "code": 400,
                "error": error
            })
        } else {
            res.json({
                "code": 200,
                "data": result
            })
        }
    })
})

// 将要购买的商品数量添加
router.post('/goods_step', (req, res) => {
    const num = req.body.num;
    const id = req.body.id;
    db.query(`update gm_goods set num = ${num} where id = ${id}`, (error, result) => {
        if (error) {
            console.log(error);

        }
    })

})

module.exports = router